package com.servlet;

import com.alibaba.fastjson.JSON;
import com.dao.Impl.CardtypeDaoImpl;
import com.dao.Impl.GoodsDaoImpl;
import com.dao.Impl.OrderDaoImpl;
import com.dao.Impl.OrderinfoDaoImpl;
import com.dao.OrderDao;
import com.entity.Cardtype;
import com.entity.Order;
import com.entity.Orderinfo;
import com.entity.PageBean;
import com.util.MyUtil;
import com.util.TransBean;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/orderController/*")
public class OrderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String uri = request.getRequestURI();
        String target = "";
        String method = uri.substring(uri.lastIndexOf("/") + 1);
        switch (method) {
            case "select":
                target = select(request, response);
                break;
            case "show":
                target = show(request, response);
                break;
            case "findByOrderId":
                target = findByOrderId(request, response);
                break;
            case "shenHe":
                target = shenHe(request, response);
                break;
            case "shenHeqr":
                target = shenHeqr(request, response);
                break;
            case "findExpenseRecord":
                target = findExpenseRecord(request, response);
                break;
            default:
                break;

        }
        if (target != null && target != "") {
            request.getRequestDispatcher(target).forward(request, response);
        }

    }

    private String shenHeqr(HttpServletRequest request, HttpServletResponse response) {
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        int i = new OrderDaoImpl().updateOrderStatus(orderId);
        MyUtil.jsonWriter(response,i);
        return null;
    }

    private String shenHe(HttpServletRequest request, HttpServletResponse response) {
        Order order = new OrderDaoImpl().findByOrderId(Integer.parseInt(request.getParameter("orderId")));
        if (order.getCardType() != null) {
            order.setCardtype(new CardtypeDaoImpl().findByLevelId(order.getCardType()));
        }
        List<Orderinfo> infos = new OrderinfoDaoImpl().findByOrderId(order.getOrderId());
        if(infos!=null&&infos.size()>0){
            for (Orderinfo info : infos) {
                info.setGoods(new GoodsDaoImpl().findByGoodsId(info.getGoodsId()));
            }
        }
        Map map = new HashMap();
        map.put("order",order);
        map.put("infos",infos);
        MyUtil.jsonWriter(response,map);
        return null;
    }

    private String show(HttpServletRequest request, HttpServletResponse response) {
        PageBean pageBean = new PageBean();
        TransBean.populate(pageBean, request.getParameterMap());
        new OrderDaoImpl().findShow(pageBean);
        List<Order> orders = pageBean.getData();
        if (orders != null) {
            for (Order order : orders) {
                if (order.getCardType() != null) {
                    order.setCardtype(new CardtypeDaoImpl().findByLevelId(order.getCardType()));
                }
            }
        }
        MyUtil.jsonWriter(response, pageBean);
        return null;
    }

    private String findByOrderId(HttpServletRequest request, HttpServletResponse response) {
        Order order = new OrderDaoImpl().findByOrderId(Integer.parseInt(request.getParameter("orderId")));
        if (order.getCardType() != null) {
            order.setCardtype(new CardtypeDaoImpl().findByLevelId(order.getCardType()));
        }
        MyUtil.jsonWriter(response, order);
        return null;
    }

    //  查询每个月每个月的消费额，做统计图用
    private String select(HttpServletRequest request, HttpServletResponse response) {
        OrderDao dao = new OrderDaoImpl();
        List<Order> orders = dao.findSum();
        Map map = new HashMap();
        for (Order order : orders) {
            map.put(order.getMonth(), order.getPriceSum());
        }
        for (int i = 1; i <= 12; i++) {
            if (map.get(i) == null) {
                map.put(i, 0);
            }
        }
        request.setAttribute("orders", JSON.toJSONString(map));
        return "/statistics/sales.jsp";

    }

    /**
     * 查询每个人的消费记录
     *
     * @param request
     * @param response
     * @return
     */
    private String findExpenseRecord(HttpServletRequest request, HttpServletResponse response) {
        PageBean<Map> pageBean = new PageBean<>();
        pageBean.setPageNo(Integer.parseInt(request.getParameter("pageBean[pageNo]")));
        pageBean.setPageSize(Integer.parseInt(request.getParameter("pageBean[pageSize]")));
        int cardId = Integer.parseInt(request.getParameter("cardId"));
        OrderDao dao = new OrderDaoImpl();
        dao.findConsume(pageBean, cardId);
        MyUtil.jsonWriter(response, pageBean);
        return null;
    }


}
